import { Component, OnInit, AfterViewInit } from '@angular/core';
import { CommService } from './../../comm/comm.service';
import 'rxjs/add/operator/map';
import { ReportUrls } from '../../comm/report.urls.conf';
import { QueryModel } from '../../model/report/query-model';
import { GlobalDataManager } from 'app/ptpmc/comm/global-data-manager';
import { Notifycation } from '../../comm/notifycation';
@Component({
    selector: 'app-config-phcnt',
    templateUrl: './config-phcnt.component.html',
    styleUrls: ['./config-phcnt.component.css'],
})

export class ConfigPhcntComponent implements OnInit, AfterViewInit {

    title = '小时产量模版配置';

    public items: any[] = [];

    public dialogItems1: any[] = [];
    public dialogItems2: any[] = [];
    public dialogItems3: any[] = [];

    public templateItems: any[] = [];
    public templateSel;

    public templateItems1: any[] = [];
    public templateSel1;

    public subAreas: any[] = GlobalDataManager.getSubAreas().map(e => e.subArea);
    public tempSelectAreas: any[] = [];

    public filterItems = [];

    public templateName = '';

    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        // 初始化模版下拉列表
        this.initTemplate();

        for (let i = 0; i < 12; i++) {
            const m1 = {
                templateName: '',
                shiftId: '1',
                hour: i + 1,
                jph: '',
                grossJPH: '',
                workTime: '',
                restTime: ''
            };
            const m2 = {
                templateName: '',
                shiftId: '2',
                hour: i + 1,
                jph: '',
                grossJPH: '',
                workTime: '',
                restTime: ''
            };
            const m3 = {
                templateName: '',
                shiftId: '3',
                hour: i + 1,
                jph: '',
                grossJPH: '',
                workTime: '',
                restTime: ''
            };
            this.dialogItems1.push(m1);
            this.dialogItems2.push(m2);
            this.dialogItems3.push(m3);
        }

        this.initFilterItems();

    }

    ngAfterViewInit() {
    }

    initFilterItems() {
        this.commServices.doPost('/prodCount/filterv/list', {}).subscribe(e => this.filterItems = e.data);
    }

    updateFilterValue() {
        this.commServices.doPost('/prodCount/filterv/update', this.filterItems).subscribe(() => {
            Notifycation.showNotification('更新完成', 'info');
        });
    }

    initTemplate() {
        this.commServices
            .getList(null, ReportUrls.ProdHourTemplate)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.templateItems = model.data;
                    this.templateSel = this.templateItems[0];
                    this.templateItems1 = model.data;
                    this.templateSel1 = this.templateItems[0];
                    this.getReportData();
                } else {
                    alert(model.resultMessage);
                }
            });
    }

    /**获取表格数据*/
    getReportData(): void {

        this.tempSelectAreas = [];
        this.commServices
            .doGet(ReportUrls.ProdHourTemplateAreas + '/' + this.templateSel.templateName)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.tempSelectAreas = model.data.map(e => e.subArea);
                } else {
                    alert(model.resultMessage);
                }
            });

        this.items = [];
        this.commServices
            .doGet(ReportUrls.ProdHourTemplateDetail + '/' + this.templateSel.templateName)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.items = model.data;
                    this.templateName = this.items[0].templateName;
                } else {
                    alert(model.resultMessage);
                }
            });

        this.initFilterItems();
    }

    onTemplateSelectedChange(template: any) {
        if (template != null) {
            this.commServices
                .doGet(ReportUrls.ProdHourTemplateDetail + '/' + template.value.templateName)
                .subscribe(model => {
                    if (model.resultCode === 200) {
                        const items = model.data;
                        this.dialogItems1 = items.filter(e => e.shiftId === 1);
                        this.dialogItems2 = items.filter(e => e.shiftId === 2);
                        this.dialogItems3 = items.filter(e => e.shiftId === 3);
                    } else {
                        alert(model.resultMessage);
                    }
                });
        }
    }

    areaIsChecked(area: any) {
        return this.tempSelectAreas.find(e => e === area) !== undefined;
    }

    /**
     * 添加产量作息模版
     */
    saveTemplate() {
        const templateName = prompt('请输入模版名称（支持大小写字母，数字，下划线，中划线，空格）');
        if (templateName !== null && templateName !== undefined && templateName !== '') {

            const reg = /^[a-zA-Z0-9_-\s*]{1,}$/;
            if(!templateName.match(reg)){
                alert('模板名称中只能出现[支持大小写字母，数字，下划线，中划线，空格]');
                return;
            }

            let a = new Array();
            a = a.concat(this.dialogItems1);
            a = a.concat(this.dialogItems2);
            a = a.concat(this.dialogItems3);

            a.forEach(e => {
                e.newTemplateName = templateName;
                e.templateName = templateName;
            });

            this.commServices
                .getList(a, ReportUrls.ProdHourAddTemplate)
                .subscribe(model => {
                    if (model.resultCode === 200) {
                        this.initTemplate();
                        alert('添加成功!');
                    } else {
                        alert(model.resultMessage);
                    }
                });
        } else {
            alert('模版名称不能为空!');
        }
    }

    updateTemplate() {
        this.items.forEach(e => e.newTemplateName = this.templateName);
        this.commServices
            .getList(this.items, ReportUrls.ProdHourAddTemplate)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    // this.getReportData();
                    this.initTemplate();
                    alert('更新成功!');
                } else {
                    alert(model.resultMessage);
                }
            });
    }

    // 选择启用区域
    chooseArea(area: any) {
        if (this.tempSelectAreas.find(e => e === area) !== undefined) {
            this.tempSelectAreas = this.tempSelectAreas.filter(e => e !== area);
        } else {
            this.tempSelectAreas.push(area);
        }
    }

    saveAreas() {
        const queryMoel = new QueryModel();
        queryMoel.tag = this.templateSel.templateName;
        queryMoel.updateSubAreas = this.tempSelectAreas;
        this.commServices
            .getList(queryMoel, ReportUrls.ProdHourTemplateDelAreas)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    alert('更新成功!');
                } else {
                    alert(model.resultMessage);
                }
            });
    }

    delTempalte() {
        if (confirm('确认删除？删除后不能恢复！')) {
            this.commServices
                .getList({
                    templateName: this.templateSel1.templateName
                }, '/prodCountTemplate/del/template')
                .subscribe(model => {
                    if (model.resultCode === 200) {
                        alert('删除成功!');
                        this.templateItems1 = this.templateItems1.filter(e => e.templateName !== this.templateSel1.templateName);
                        this.templateItems = this.templateItems.filter(e => e.templateName !== this.templateSel1.templateName);
                    } else {
                        alert(model.resultMessage);
                    }
                });
        }
    }


}
